package com.ruoyi.system.controller.cooperation;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.common.BillTypeConstant;
import com.ruoyi.system.domain.db.cooperation.ExternalCooperation;
import com.ruoyi.system.domain.db.financial.FinancialEconomic;
import com.ruoyi.system.domain.vo.ExternalCooperationVo;
import com.ruoyi.system.domain.vo.PageInfo;
import com.ruoyi.system.service.IExternalCooperationService;
import com.ruoyi.system.service.IFinancialEconomicService;
import com.ruoyi.system.utils.MyPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cooperation/advertisement")
public class CooperationOrderController extends BaseController {

    @Autowired
    private IExternalCooperationService externalCooperationService;

    @Autowired
    private IFinancialEconomicService financialEconomicService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('cooperation:advertisement:list')")
    @GetMapping("/list")
    public PageInfo<ExternalCooperationVo> list(ExternalCooperation externalCooperation){
        //startPage();
        List<ExternalCooperationVo> list = externalCooperationService.selectExternalCooperationOrderList(externalCooperation);
        return MyPageUtils.startPage(list);
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('cooperation:advertisement:query')")
    @GetMapping("")
    public AjaxResult getInfo(@RequestParam(required = false) Long orderId)
    {
        return success(externalCooperationService.selectExternalCooperationOrderByOrderId(orderId));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('cooperation:advertisement:add')")
    @Log(title = "对外合作管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExternalCooperation externalCooperation)
    {
        int result = externalCooperationService.insertExternalCooperationOrder(externalCooperation);
        FinancialEconomic financialEconomic = new FinancialEconomic(
                BillTypeConstant.COOPERATION_ADVERTISEMENT,externalCooperation.getAdvertiseId(),
                externalCooperation.getSalary(),externalCooperation.getRemark() // 备注从remark改过去
        );
        financialEconomicService.insertFinancialEconomic(financialEconomic);
        return toAjax(result);
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('cooperation:advertisement:edit')")
    @Log(title = "对外合作管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExternalCooperation externalCooperation)
    {
        boolean flag = false;
        int result = externalCooperationService.updateExternalCooperationOrder(externalCooperation);
        FinancialEconomic financialEconomic = financialEconomicService.getBillByOrderIdAndBillTypeId(externalCooperation.getAdvertiseId(), BillTypeConstant.COOPERATION_ADVERTISEMENT);
        if (!financialEconomic.getSalary().equals(externalCooperation.getSalary())) {
            financialEconomic.setSalary(externalCooperation.getSalary());
            flag = true;
        }
        if (financialEconomic.getInfo() == null) {
            financialEconomic.setInfo("");
        }
        if (!financialEconomic.getInfo().equals(externalCooperation.getRemark())) {
            financialEconomic.setInfo(externalCooperation.getRemark());
            flag = true;
        }
        if (flag) {  // 变了就改，不然不改
            financialEconomicService.updateFinancialEconomic(financialEconomic);
        }
        return toAjax(result);
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('cooperation:advertisement:remove')")
    @Log(title = "对外合作管理", businessType = BusinessType.DELETE)
    @DeleteMapping("")
    public AjaxResult remove(@RequestBody List<Long> orderIds)
    {
        List<Long> billIds = new ArrayList<>();
        for (Long orderId : orderIds) {
            Long billId = financialEconomicService.getBillByOrderIdAndBillTypeId(orderId, BillTypeConstant.COOPERATION_ADVERTISEMENT).getBillId();
            billIds.add(billId);
        }
        financialEconomicService.deleteFinancialEconomicByOrderIds(billIds);
        return toAjax(externalCooperationService.deleteExternalCooperationOrderByOrderIds(orderIds));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('cooperation:advertisement:removeByid')")
    @Log(title = "对外合作管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderId}")
    public AjaxResult removeById(@PathVariable Long orderId)
    {
        FinancialEconomic financialEconomic = financialEconomicService.getBillByOrderIdAndBillTypeId(orderId, BillTypeConstant.COOPERATION_ADVERTISEMENT);
        financialEconomicService.deleteFinancialEconomicByOrderId(financialEconomic.getBillId());
        return toAjax(externalCooperationService.deleteExternalCooperationOrderByOrderId(orderId));
    }
}
